// 交换输出两个数的值
#include <iostream>

using namespace std;

// 单例模式
class SWAP
{
private:
    int num1;
    int num2;

private:
    // 构造函数私有化
    SWAP();
    SWAP(int num1, int num2);
    SWAP(const SWAP &ob);
    ~SWAP();

private:
    static SWAP * const swap;
public:
    // 推出指针
    static SWAP *pushPointer(void);
public:
    // 重载运算符
    SWAP *operator->()
    {
        return this->swap;
    }

    SWAP operator*()
    {
        return *swap;
    }

public:
    int getNum1(void);
    int getNum2(void);
    int *getNum1Addr(void);
    int *getNum2Addr(void);
    void assignNum1(int a);
    void assignNum2(int a);
    void mySwap(void);
};

SWAP * const SWAP::swap = new SWAP;

int main(int argc, char **argv)
{
    int a = 0;
    int b = 0;
    // 获取单例模式指针
    SWAP *p = SWAP::pushPointer();

    // 人机交互
    cout << "请输入" << endl;
    cin >> a >> b;

    // 输入
    p->assignNum1(a);
    p->assignNum2(b);

    // 交换
    (*p).mySwap();

    // 输出
    cout << "a = " << p->getNum1() << " " <<  "b = " << p->getNum2() << endl; 

    return 0;
}

SWAP::SWAP()
{
    /* 无参构造 */
    this->num1 = 0;
    this->num2 = 0;
}

SWAP::SWAP(int num1, int num2)
{
    /* 有参构造 */
    this->num1 = num1;
    this->num2 = num2;
}

SWAP::~SWAP()
{
    /* 析构函数 */
}

SWAP::SWAP(const SWAP &ob)
{
    /* 拷贝构造 */
    this->num1 = ob.num1;
    this->num2 = ob.num2;
}

SWAP *SWAP::pushPointer(void)
{
    return swap;
}

int SWAP::getNum1(void)
{
    return this->num1;
}

int SWAP::getNum2(void)
{
    return this->num2;
}

int *SWAP::getNum1Addr(void)
{
    return &(this->num1);
}

int *SWAP::getNum2Addr(void)
{
    return &(this->num2);
}

void SWAP::assignNum1(int a)
{
    this->num1 = a;
    return;
}

void SWAP::assignNum2(int a)
{
    this->num2 = a;
    return;
}

void SWAP::mySwap(void)
{
    int tmp = num1;
    num1 = num2;
    num2 = tmp;
    return;
}